package com.kang.commons;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.kang.commons.exception.MyException;
import lombok.Data;

/**
 * @program: photo_fawu
 * @description: 规范返回结果
 * @author: K.faWu
 * @create: 2021-10-25 15:26
 **/

@Data
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;


    public static <T> Result<T> success(){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("success");
        return result;
    }
    public static <T> Result<T> failure(){
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setMsg("error");
        return result;
    }

    public static <T> Result<T> success(T data){
        Result<T> result = success();
        result.setData(data);
        return result;
    }
    public static <T> Result<T> failure(String msg){
        Result<T> result = failure();
        result.setMsg(msg);
        return result;
    }
    public static<T> Result<T> failure(String msg, T data){
        Result<T> result = failure(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String msg){
        throw new MyException(500,msg);
    }

}
